home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16404 < prev    next >
Encoding:
Text File  |  1996-08-05  |  30.0 KB  |  707 lines

  1. Path: gaia.ns.utk.edu!mbk
  2. From: mbk@caffeine.engr.utk.edu (Matt Kennel)
  3. Newsgroups: comp.lang.smalltalk,comp.object,comp.lang.c++,comp.lang.java
  4. Subject: Re: The Good, the Bad, the Ugly, and the Wicked ...
  5. Followup-To: comp.lang.smalltalk,comp.object,comp.lang.c++,comp.lang.java
  6. Date: 10 Apr 1996 18:41:38 GMT
  7. Organization: University of Tennessee, Knoxville and Oak Ridge National Laboratory
  8. Message-ID: <4kgvd2$nga@gaia.ns.utk.edu>
  9. References: <31570B8E.5A12@vmark.com> <4je5rq$7qg@mimas.brunel.ac.uk> <RMARTIN.96Apr9160010@rcm.oma.com> <goochb.325.00160E30@rwi.com>
  10. Reply-To: kennel@msr.epm.ornl.gov
  11. NNTP-Posting-Host: caffeine.engr.utk.edu
  12. X-Newsreader: TIN [version 1.2 PL2]
  13.  
  14. William D. Gooch (goochb@rwi.com) wrote:
  15. : In article <RMARTIN.96Apr9160010@rcm.oma.com> rmartin@oma.com (Robert C. Martin) writes:
  16.  
  17. : >In article <4kbq3q$1i8@gaia.ns.utk.edu> mbk@caffeine.engr.utk.edu (Matt Kennel)
  18. : >writes:
  19.  
  20. : >   Robert C. Martin (rmartin@oma.com) wrote:
  21.  
  22.  
  23. : >In some cases.  In others it is difficult to live with.  There is no generic
  24. : >solution.
  25.  
  26. : Cute, but a litle too glib perhaps.  Since (from what you wrote earlier)
  27. : you know that a quality GC implementation allows one to turn it on 
  28. : and off, force collection, and do one's own memory management,
  29. : what basis does that leave for GC being "hard to live with?" 
  30.  
  31. Just to inject some slight amount of reality in this thread, here is the
  32. top-level 'include' file for a well regarded and freely available garbage
  33. collector for C and C++.  It has the interface to the most commonly used
  34. user-visible capabilities.  The '.a' archive stripped of debugging symbols
  35. is 80860 bytes on my Linux machine. 
  36.  
  37. You might consider this to be a 'lower bound' on the capabilities of
  38. decent commercial collectors. 
  39.  
  40. As you can see, garbage collectors are not totally 
  41.  
  42. /* 
  43.  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
  44.  * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
  45.  *
  46.  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  47.  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
  48.  *
  49.  * Permission is hereby granted to use or copy this program
  50.  * for any purpose,  provided the above notices are retained on all copies.
  51.  * Permission to modify the code and to distribute modified code is granted,
  52.  * provided the above notices are retained, and a notice that the code was
  53.  * modified is included with the above copyright notice.
  54.  */
  55. /* Boehm, January 28, 1995 3:59 pm PST */
  56.  
  57. /*
  58.  * Note that this defines a large number of tuning hooks, which can
  59.  * safely be ignored in nearly all cases.  For normal use it suffices
  60.  * to call only GC_MALLOC and perhaps GC_REALLOC.
  61.  * For better performance, also look at GC_MALLOC_ATOMIC, and
  62.  * GC_enable_incremental.  If you need an action to be performed
  63.  * immediately before an object is collected, look at GC_register_finalizer.
  64.  * If you are using Solaris threads, look at the end of this file.
  65.  * Everything else is best ignored unless you encounter performance
  66.  * problems.
  67.  */
  68.  
  69. #ifndef _GC_H
  70.  
  71. # define _GC_H
  72.  
  73. # ifdef __cplusplus
  74.     extern "C" {
  75. # endif
  76.  
  77. # include <stddef.h>
  78.  
  79. /* Define word and signed_word to be unsigned and signed types of the     */
  80. /* size as char * or void *.  There seems to be no way to do this    */
  81. /* even semi-portably.  The following is probably no better/worse     */
  82. /* than almost anything else.                        */
  83. /* The ANSI standard suggests that size_t and ptr_diff_t might be     */
  84. /* better choices.  But those appear to have incorrect definitions    */
  85. /* on may systems.  Notably "typedef int size_t" seems to be both    */
  86. /* frequent and WRONG.                            */
  87. typedef unsigned long GC_word;
  88. typedef long GC_signed_word;
  89.  
  90. /* Public read-only variables */
  91.  
  92. extern GC_word GC_gc_no;/* Counter incremented per collection.      */
  93.             /* Includes empty GCs at startup.        */
  94.             
  95.  
  96. /* Public R/W variables */
  97.  
  98. extern int GC_quiet;    /* Disable statistics output.  Only matters if    */
  99.             /* collector has been compiled with statistics    */
  100.             /* enabled.  This involves a performance cost,    */
  101.             /* and is thus not the default.            */
  102.  
  103. extern int GC_dont_gc;    /* Dont collect unless explicitly requested, e.g. */
  104.             /* beacuse it's not safe.              */
  105.  
  106. extern int GC_dont_expand;
  107.             /* Dont expand heap unless explicitly requested */
  108.             /* or forced to.                */
  109.  
  110. extern int GC_full_freq;    /* Number of partial collections between    */
  111.                 /* full collections.  Matters only if    */
  112.                 /* GC_incremental is set.            */
  113.             
  114. extern GC_word GC_non_gc_bytes;
  115.             /* Bytes not considered candidates for collection. */
  116.             /* Used only to control scheduling of collections. */
  117.  
  118. extern GC_word GC_free_space_divisor;
  119.             /* We try to make sure that we allocate at     */
  120.             /* least N/GC_free_space_divisor bytes between    */
  121.             /* collections, where N is the heap size plus    */
  122.             /* a rough estimate of the root set size.    */
  123.             /* Initially, GC_free_space_divisor = 4.    */
  124.             /* Increasing its value will use less space    */
  125.             /* but more collection time.  Decreasing it    */
  126.             /* will appreciably decrease collection time    */
  127.             /* at the expense of space.            */
  128.             /* GC_free_space_divisor = 1 will effectively    */
  129.             /* disable collections.                */
  130.             
  131.             
  132. /* Public procedures */
  133. /*
  134.  * general purpose allocation routines, with roughly malloc calling conv.
  135.  * The atomic versions promise that no relevant pointers are contained
  136.  * in the object.  The nonatomic versions guarantee that the new object
  137.  * is cleared.  GC_malloc_stubborn promises that no changes to the object
  138.  * will occur after GC_end_stubborn_change has been called on the
  139.  * result of GC_malloc_stubborn. GC_malloc_uncollectable allocates an object
  140.  * that is scanned for pointers to collectable objects, but is not itself
  141.  * collectable.  GC_malloc_uncollectable and GC_free called on the resulting
  142.  * object implicitly update GC_non_gc_bytes appropriately.
  143.  */
  144. # if defined(__STDC__) || defined(__cplusplus)
  145.   extern void * GC_malloc(size_t size_in_bytes);
  146.   extern void * GC_malloc_atomic(size_t size_in_bytes);
  147.   extern void * GC_malloc_uncollectable(size_t size_in_bytes);
  148.   extern void * GC_malloc_stubborn(size_t size_in_bytes);
  149. # else
  150.   extern char * GC_malloc(/* size_in_bytes */);
  151.   extern char * GC_malloc_atomic(/* size_in_bytes */);
  152.   extern char * GC_malloc_uncollectable(/* size_in_bytes */);
  153.   extern char * GC_malloc_stubborn(/* size_in_bytes */);
  154. # endif
  155.  
  156. #if defined(__STDC__) && !defined(__cplusplus)
  157. # define NO_PARAMS void
  158. #else
  159. # define NO_PARAMS
  160. #endif
  161.  
  162. /* Explicitly deallocate an object.  Dangerous if used incorrectly.     */
  163. /* Requires a pointer to the base of an object.                */
  164. /* If the argument is stubborn, it should not be changeable when freed. */
  165. /* An object should not be enable for finalization when it is         */
  166. /* explicitly deallocated.                        */
  167. /* GC_free(0) is a no-op, as required by ANSI C for free.        */
  168. # if defined(__STDC__) || defined(__cplusplus)
  169.   extern void GC_free(void * object_addr);
  170. # else
  171.   extern void GC_free(/* object_addr */);
  172. # endif
  173.  
  174. /*
  175.  * Stubborn objects may be changed only if the collector is explicitly informed.
  176.  * The collector is implicitly informed of coming change when such
  177.  * an object is first allocated.  The following routines inform the
  178.  * collector that an object will no longer be changed, or that it will
  179.  * once again be changed.  Only nonNIL pointer stores into the object
  180.  * are considered to be changes.  The argument to GC_end_stubborn_change
  181.  * must be exacly the value returned by GC_malloc_stubborn or passed to
  182.  * GC_change_stubborn.  (In the second case it may be an interior pointer
  183.  * within 512 bytes of the beginning of the objects.)
  184.  * There is a performance penalty for allowing more than
  185.  * one stubborn object to be changed at once, but it is acceptable to
  186.  * do so.  The same applies to dropping stubborn objects that are still
  187.  * changeable.
  188.  */
  189. void GC_change_stubborn(/* p */);
  190. void GC_end_stubborn_change(/* p */);
  191.  
  192. /* Return a pointer to the base (lowest address) of an object given    */
  193. /* a pointer to a location within the object.                */
  194. /* Return 0 if displaced_pointer doesn't point to within a valid    */
  195. /* object.                                */
  196. # if defined(__STDC__) || defined(__cplusplus)
  197.   void * GC_base(void * displaced_pointer);
  198. # else
  199.   char * GC_base(/* char * displaced_pointer */);
  200. # endif
  201.  
  202. /* Given a pointer to the base of an object, return its size in bytes.    */
  203. /* The returned size may be slightly larger than what was originally    */
  204. /* requested.                                */
  205. # if defined(__STDC__) || defined(__cplusplus)
  206.   size_t GC_size(void * object_addr);
  207. # else
  208.   size_t GC_size(/* char * object_addr */);
  209. # endif
  210.  
  211. /* For compatibility with C library.  This is occasionally faster than    */
  212. /* a malloc followed by a bcopy.  But if you rely on that, either here    */
  213. /* or with the standard C library, your code is broken.  In my        */
  214. /* opinion, it shouldn't have been invented, but now we're stuck. -HB    */
  215. /* The resulting object has the same kind as the original.        */
  216. /* If the argument is stubborn, the result will have changes enabled.    */
  217. /* It is an error to have changes enabled for the original object.    */
  218. /* Follows ANSI comventions for NULL old_object.            */
  219. # if defined(__STDC__) || defined(__cplusplus)
  220.     extern void * GC_realloc(void * old_object, size_t new_size_in_bytes);
  221. # else
  222.     extern char * GC_realloc(/* old_object, new_size_in_bytes */);
  223. # endif
  224.  
  225.  
  226. /* Explicitly increase the heap size.    */
  227. /* Returns 0 on failure, 1 on success.  */
  228. extern int GC_expand_hp(/* number_of_bytes */);
  229.  
  230. /* Limit the heap size to n bytes.  Useful when you're debugging,     */
  231. /* especially on systems that don't handle running out of memory well.    */
  232. /* n == 0 ==> unbounded.  This is the default.                */
  233. extern void GC_set_max_heap_size(/* n */);
  234.  
  235. /* Clear the set of root segments.  Wizards only. */
  236. extern void GC_clear_roots(NO_PARAMS);
  237.  
  238. /* Add a root segment.  Wizards only. */
  239. extern void GC_add_roots(/* low_address, high_address_plus_1 */);
  240.  
  241. /* Add a displacement to the set of those considered valid by the    */
  242. /* collector.  GC_register_displacement(n) means that if p was returned */
  243. /* by GC_malloc, then (char *)p + n will be considered to be a valid    */
  244. /* pointer to n.  N must be small and less than the size of p.        */
  245. /* (All pointers to the interior of objects from the stack are        */
  246. /* considered valid in any case.  This applies to heap objects and    */
  247. /* static data.)                            */
  248. /* Preferably, this should be called before any other GC procedures.    */
  249. /* Calling it later adds to the probability of excess memory        */
  250. /* retention.                                */
  251. /* This is a no-op if the collector was compiled with recognition of    */
  252. /* arbitrary interior pointers enabled, which is now the default.    */
  253. void GC_register_displacement(/* GC_word n */);
  254.  
  255. /* The following version should be used if any debugging allocation is    */
  256. /* being done.                                */
  257. void GC_debug_register_displacement(/* GC_word n */);
  258.  
  259. /* Explicitly trigger a full, world-stop collection.     */
  260. void GC_gcollect(NO_PARAMS);
  261.  
  262. /* Trigger a full world-stopped collection.  Abort the collection if     */
  263. /* and when stop_func returns a nonzero value.  Stop_func will be     */
  264. /* called frequently, and should be reasonably fast.  This works even    */
  265. /* if virtual dirty bits, and hence incremental collection is not     */
  266. /* available for this architecture.  Collections can be aborted faster    */
  267. /* than normal pause times for incremental collection.  However,    */
  268. /* aborted collections do no useful work; the next collection needs    */
  269. /* to start from the beginning.                        */
  270. typedef int (* GC_stop_func)(NO_PARAMS);
  271. # if defined(__STDC__) || defined(__cplusplus)
  272.     int GC_try_to_collect(GC_stop_func stop_func);
  273. # else
  274.     int GC_try_to_collect(/* GC_stop_func stop_func */);
  275. # endif
  276.  
  277. /* Return the number of bytes in the heap.  Excludes collector private    */
  278. /* data structures.  Includes empty blocks and fragmentation loss.    */
  279. /* Includes some pages that were allocated but never written.        */
  280. size_t GC_get_heap_size(NO_PARAMS);
  281.  
  282. /* Return the number of bytes allocated since the last collection.    */
  283. size_t GC_get_bytes_since_gc(NO_PARAMS);
  284.  
  285. /* Enable incremental/generational collection.    */
  286. /* Not advisable unless dirty bits are         */
  287. /* available or most heap objects are        */
  288. /* pointerfree(atomic) or immutable.        */
  289. /* Don't use in leak finding mode.        */
  290. /* Ignored if GC_dont_gc is true.        */
  291. void GC_enable_incremental(NO_PARAMS);
  292.  
  293. /* Perform some garbage collection work, if appropriate.    */
  294. /* Return 0 if there is no more work to be done.        */
  295. /* Typically performs an amount of work corresponding roughly    */
  296. /* to marking from one page.  May do more work if further    */
  297. /* progress requires it, e.g. if incremental collection is    */
  298. /* disabled.  It is reasonable to call this in a wait loop    */
  299. /* until it returns 0.                        */
  300. int GC_collect_a_little(NO_PARAMS);
  301.  
  302. /* Allocate an object of size lb bytes.  The client guarantees that    */
  303. /* as long as the object is live, it will be referenced by a pointer    */
  304. /* that points to somewhere within the first 256 bytes of the object.    */
  305. /* (This should normally be declared volatile to prevent the compiler    */
  306. /* from invalidating this assertion.)  This routine is only useful    */
  307. /* if a large array is being allocated.  It reduces the chance of     */
  308. /* accidentally retaining such an array as a result of scanning an    */
  309. /* integer that happens to be an address inside the array.  (Actually,    */
  310. /* it reduces the chance of the allocator not finding space for such    */
  311. /* an array, since it will try hard to avoid introducing such a false    */
  312. /* reference.)  On a SunOS 4.X or MS Windows system this is recommended */
  313. /* for arrays likely to be larger than 100K or so.  For other systems,    */
  314. /* or if the collector is not configured to recognize all interior    */
  315. /* pointers, the threshold is normally much higher.            */
  316. # if defined(__STDC__) || defined(__cplusplus)
  317.   void * GC_malloc_ignore_off_page(size_t lb);
  318. # else
  319.   char * GC_malloc_ignore_off_page(/* size_t lb */);
  320. # endif
  321. # if defined(__STDC__) || defined(__cplusplus)
  322.   void * GC_malloc_atomic_ignore_off_page(size_t lb);
  323. # else
  324.   char * GC_malloc_atomic_ignore_off_page(/* size_t lb */);
  325. # endif
  326.  
  327. /* Debugging (annotated) allocation.  GC_gcollect will check         */
  328. /* objects allocated in this way for overwrites, etc.            */
  329. # if defined(__STDC__) || defined(__cplusplus)
  330.   extern void * GC_debug_malloc(size_t size_in_bytes,
  331.                   char * descr_string, int descr_int);
  332.   extern void * GC_debug_malloc_atomic(size_t size_in_bytes,
  333.                          char * descr_string, int descr_int);
  334.   extern void * GC_debug_malloc_uncollectable(size_t size_in_bytes,
  335.                              char * descr_string, int descr_int);
  336.   extern void * GC_debug_malloc_stubborn(size_t size_in_bytes,
  337.                            char * descr_string, int descr_int);
  338.   extern void GC_debug_free(void * object_addr);
  339.   extern void * GC_debug_realloc(void * old_object,
  340.                     size_t new_size_in_bytes,
  341.                     char * descr_string, int descr_int);
  342. # else
  343.   extern char * GC_debug_malloc(/* size_in_bytes, descr_string, descr_int */);
  344.   extern char * GC_debug_malloc_atomic(/* size_in_bytes, descr_string,
  345.                         descr_int */);
  346.   extern char * GC_debug_malloc_uncollectable(/* size_in_bytes, descr_string,
  347.                         descr_int */);
  348.   extern char * GC_debug_malloc_stubborn(/* size_in_bytes, descr_string,
  349.                         descr_int */);
  350.   extern void GC_debug_free(/* object_addr */);
  351.   extern char * GC_debug_realloc(/* old_object, new_size_in_bytes,
  352.                           descr_string, descr_int */);
  353. # endif
  354. void GC_debug_change_stubborn(/* p */);
  355. void GC_debug_end_stubborn_change(/* p */);
  356. # ifdef GC_DEBUG
  357. #   define GC_MALLOC(sz) GC_debug_malloc(sz, __FILE__, __LINE__)
  358. #   define GC_MALLOC_ATOMIC(sz) GC_debug_malloc_atomic(sz, __FILE__, __LINE__)
  359. #   define GC_MALLOC_UNCOLLECTABLE(sz) GC_debug_malloc_uncollectable(sz, \
  360.                             __FILE__, __LINE__)
  361. #   define GC_REALLOC(old, sz) GC_debug_realloc(old, sz, __FILE__, \
  362.                                    __LINE__)
  363. #   define GC_FREE(p) GC_debug_free(p)
  364. #   define GC_REGISTER_FINALIZER(p, f, d, of, od) \
  365.     GC_register_finalizer(GC_base(p), GC_debug_invoke_finalizer, \
  366.                   GC_make_closure(f,d), of, od)
  367. #   define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
  368.     GC_register_finalizer_ignore_self( \
  369.              GC_base(p), GC_debug_invoke_finalizer, \
  370.          GC_make_closure(f,d), of, od)
  371. #   define GC_MALLOC_STUBBORN(sz) GC_debug_malloc_stubborn(sz, __FILE__, \
  372.                                    __LINE__)
  373. #   define GC_CHANGE_STUBBORN(p) GC_debug_change_stubborn(p)
  374. #   define GC_END_STUBBORN_CHANGE(p) GC_debug_end_stubborn_change(p)
  375. #   define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
  376.     GC_general_register_disappearing_link(link, GC_base(obj))
  377. #   define GC_REGISTER_DISPLACEMENT(n) GC_debug_register_displacement(n)
  378. # else
  379. #   define GC_MALLOC(sz) GC_malloc(sz)
  380. #   define GC_MALLOC_ATOMIC(sz) GC_malloc_atomic(sz)
  381. #   define GC_MALLOC_UNCOLLECTABLE(sz) GC_malloc_uncollectable(sz)
  382. #   define GC_REALLOC(old, sz) GC_realloc(old, sz)
  383. #   define GC_FREE(p) GC_free(p)
  384. #   define GC_REGISTER_FINALIZER(p, f, d, of, od) \
  385.     GC_register_finalizer(p, f, d, of, od)
  386. #   define GC_REGISTER_FINALIZER_IGNORE_SELF(p, f, d, of, od) \
  387.     GC_register_finalizer_ignore_self(p, f, d, of, od)
  388. #   define GC_MALLOC_STUBBORN(sz) GC_malloc_stubborn(sz)
  389. #   define GC_CHANGE_STUBBORN(p) GC_change_stubborn(p)
  390. #   define GC_END_STUBBORN_CHANGE(p) GC_end_stubborn_change(p)
  391. #   define GC_GENERAL_REGISTER_DISAPPEARING_LINK(link, obj) \
  392.     GC_general_register_disappearing_link(link, obj)
  393. #   define GC_REGISTER_DISPLACEMENT(n) GC_register_displacement(n)
  394. # endif
  395. /* The following are included because they are often convenient, and    */
  396. /* reduce the chance for a misspecifed size argument.  But calls may    */
  397. /* expand to something syntactically incorrect if t is a complicated    */
  398. /* type expression.                              */
  399. # define GC_NEW(t) (t *)GC_MALLOC(sizeof (t))
  400. # define GC_NEW_ATOMIC(t) (t *)GC_MALLOC_ATOMIC(sizeof (t))
  401. # define GC_NEW_STUBBORN(t) (t *)GC_MALLOC_STUBBORN(sizeof (t))
  402. # define GC_NEW_UNCOLLECTABLE(t) (t *)GC_MALLOC_UNCOLLECTABLE(sizeof (t))
  403.  
  404. /* Finalization.  Some of these primitives are grossly unsafe.        */
  405. /* The idea is to make them both cheap, and sufficient to build        */
  406. /* a safer layer, closer to PCedar finalization.            */
  407. /* The interface represents my conclusions from a long discussion    */
  408. /* with Alan Demers, Dan Greene, Carl Hauser, Barry Hayes,         */
  409. /* Christian Jacobi, and Russ Atkinson.  It's not perfect, and        */
  410. /* probably nobody else agrees with it.        Hans-J. Boehm  3/13/92    */
  411. # if defined(__STDC__) || defined(__cplusplus)
  412.   typedef void (*GC_finalization_proc)(void * obj, void * client_data);
  413. # else
  414.   typedef void (*GC_finalization_proc)(/* void * obj, void * client_data */);
  415. # endif
  416.  
  417. # if defined(__STDC__) || defined(__cplusplus)
  418.     void GC_register_finalizer(void * obj,
  419.                    GC_finalization_proc fn, void * cd,
  420.                    GC_finalization_proc *ofn, void ** ocd);
  421. # else    
  422.     void GC_register_finalizer(/* void * obj,
  423.                       GC_finalization_proc fn, void * cd,
  424.                       GC_finalization_proc *ofn, void ** ocd */);
  425. # endif
  426.     /* When obj is no longer accessible, invoke        */
  427.     /* (*fn)(obj, cd).  If a and b are inaccessible, and    */
  428.     /* a points to b (after disappearing links have been    */
  429.     /* made to disappear), then only a will be        */
  430.     /* finalized.  (If this does not create any new        */
  431.     /* pointers to b, then b will be finalized after the    */
  432.     /* next collection.)  Any finalizable object that    */
  433.     /* is reachable from itself by following one or more    */
  434.     /* pointers will not be finalized (or collected).    */
  435.     /* Thus cycles involving finalizable objects should    */
  436.     /* be avoided, or broken by disappearing links.        */
  437.     /* Fn should terminate as quickly as possible, and    */
  438.     /* defer extended computation.                */
  439.     /* All but the last finalizer registered for an object  */
  440.     /* is ignored.                        */
  441.     /* Finalization may be removed by passing 0 as fn.    */
  442.     /* The old finalizer and client data are stored in    */
  443.     /* *ofn and *ocd.                    */ 
  444.     /* Fn is never invoked on an accessible object,        */
  445.     /* provided hidden pointers are converted to real     */
  446.     /* pointers only if the allocation lock is held, and    */
  447.     /* such conversions are not performed by finalization    */
  448.     /* routines.                        */
  449.     /* If GC_register_finalizer is aborted as a result of    */
  450.     /* a signal, the object may be left with no        */
  451.     /* finalization, even if neither the old nor new    */
  452.     /* finalizer were NULL.                    */
  453.     /* Obj should be the nonNULL starting address of an     */
  454.     /* object allocated by GC_malloc or friends.        */
  455.     /* Note that any garbage collectable object referenced    */
  456.     /* by cd will be considered accessible until the    */
  457.     /* finalizer is invoked.                */
  458.  
  459. /* Another versions of the above follow.  It ignores        */
  460. /* self-cycles, i.e. pointers from a finalizable object to    */
  461. /* itself.  There is a stylistic argument that this is wrong,    */
  462. /* but it's unavoidable for C++, since the compiler may        */
  463. /* silently introduce these.  It's also benign in that specific    */
  464. /* case.                            */
  465. # if defined(__STDC__) || defined(__cplusplus)
  466.     void GC_register_finalizer_ignore_self(void * obj,
  467.                    GC_finalization_proc fn, void * cd,
  468.                    GC_finalization_proc *ofn, void ** ocd);
  469. # else    
  470.     void GC_register_finalizer_ignore_self(/* void * obj,
  471.                       GC_finalization_proc fn, void * cd,
  472.                       GC_finalization_proc *ofn, void ** ocd */);
  473. # endif
  474.  
  475. /* The following routine may be used to break cycles between    */
  476. /* finalizable objects, thus causing cyclic finalizable        */
  477. /* objects to be finalized in the correct order.  Standard    */
  478. /* use involves calling GC_register_disappearing_link(&p),    */
  479. /* where p is a pointer that is not followed by finalization    */
  480. /* code, and should not be considered in determining         */
  481. /* finalization order.                        */
  482. # if defined(__STDC__) || defined(__cplusplus)
  483.     int GC_register_disappearing_link(void ** /* link */);
  484. # else
  485.     int GC_register_disappearing_link(/* void ** link */);
  486. # endif
  487.  
  488.     /* Link should point to a field of a heap allocated     */
  489.     /* object obj.  *link will be cleared when obj is    */
  490.     /* found to be inaccessible.  This happens BEFORE any    */
  491.     /* finalization code is invoked, and BEFORE any        */
  492.     /* decisions about finalization order are made.        */
  493.     /* This is useful in telling the finalizer that     */
  494.     /* some pointers are not essential for proper        */
  495.     /* finalization.  This may avoid finalization cycles.    */
  496.     /* Note that obj may be resurrected by another        */
  497.     /* finalizer, and thus the clearing of *link may    */
  498.     /* be visible to non-finalization code.          */
  499.     /* There's an argument that an arbitrary action should  */
  500.     /* be allowed here, instead of just clearing a pointer. */
  501.     /* But this causes problems if that action alters, or     */
  502.     /* examines connectivity.                */
  503.     /* Returns 1 if link was already registered, 0        */
  504.     /* otherwise.                        */
  505.     /* Only exists for backward compatibility.  See below:    */
  506. # if defined(__STDC__) || defined(__cplusplus)
  507.     int GC_general_register_disappearing_link(void ** /* link */, void * obj);
  508. # else
  509.     int GC_general_register_disappearing_link(/* void ** link, void * obj */);
  510. # endif
  511.     /* A slight generalization of the above. *link is    */
  512.     /* cleared when obj first becomes inaccessible.  This    */
  513.     /* can be used to implement weak pointers easily and    */
  514.     /* safely. Typically link will point to a location    */
  515.     /* holding a disguised pointer to obj.  (A pointer     */
  516.     /* inside an "atomic" object is effectively          */
  517.     /* disguised.)   In this way soft            */
  518.     /* pointers are broken before any object        */
  519.     /* reachable from them are finalized.  Each link    */
  520.     /* May be registered only once, i.e. with one obj    */
  521.     /* value.  This was added after a long email discussion */
  522.     /* with John Ellis.                    */
  523.     /* Obj must be a pointer to the first word of an object */
  524.     /* we allocated.  It is unsafe to explicitly deallocate */
  525.     /* the object containing link.  Explicitly deallocating */
  526.     /* obj may or may not cause link to eventually be    */
  527.     /* cleared.                        */
  528. # if defined(__STDC__) || defined(__cplusplus)
  529.     int GC_unregister_disappearing_link(void ** /* link */);
  530. # else
  531.     int GC_unregister_disappearing_link(/* void ** link */);
  532. # endif
  533.     /* Returns 0 if link was not actually registered.    */
  534.     /* Undoes a registration by either of the above two    */
  535.     /* routines.                        */
  536.  
  537. /* Auxiliary fns to make finalization work correctly with displaced    */
  538. /* pointers introduced by the debugging allocators.            */
  539. # if defined(__STDC__) || defined(__cplusplus)
  540.     void * GC_make_closure(GC_finalization_proc fn, void * data);
  541.     void GC_debug_invoke_finalizer(void * obj, void * data);
  542. # else
  543.     char * GC_make_closure(/* GC_finalization_proc fn, char * data */);
  544.     void GC_debug_invoke_finalizer(/* void * obj, void * data */);
  545. # endif
  546.  
  547. /* GC_set_warn_proc can be used to redirect or filter warning messages.    */
  548. # if defined(__STDC__) || defined(__cplusplus)
  549.     typedef void (*GC_warn_proc)(char *msg, GC_word arg);
  550.     GC_warn_proc GC_set_warn_proc(GC_warn_proc p);
  551.     /* Returns old warning procedure.    */
  552. # else
  553.     typedef void (*GC_warn_proc)(/* char *msg, GC_word arg */);
  554.     GC_warn_proc GC_set_warn_proc(/* GC_warn_proc p */);
  555. # endif
  556.     
  557. /* The following is intended to be used by a higher level    */
  558. /* (e.g. cedar-like) finalization facility.  It is expected    */
  559. /* that finalization code will arrange for hidden pointers to    */
  560. /* disappear.  Otherwise objects can be accessed after they    */
  561. /* have been collected.                        */
  562. /* Note that putting pointers in atomic objects or in         */
  563. /* nonpointer slots of "typed" objects is equivalent to     */
  564. /* disguising them in this way, and may have other advantages.    */
  565. # ifdef I_HIDE_POINTERS
  566. #   if defined(__STDC__) || defined(__cplusplus)
  567. #     define HIDE_POINTER(p) (~(size_t)(p))
  568. #     define REVEAL_POINTER(p) ((void *)(HIDE_POINTER(p)))
  569. #   else
  570. #     define HIDE_POINTER(p) (~(unsigned long)(p))
  571. #     define REVEAL_POINTER(p) ((char *)(HIDE_POINTER(p)))
  572. #   endif
  573.     /* Converting a hidden pointer to a real pointer requires verifying    */
  574.     /* that the object still exists.  This involves acquiring the      */
  575.     /* allocator lock to avoid a race with the collector.        */
  576.  
  577. #   if defined(__STDC__) || defined(__cplusplus)
  578.         typedef void * (*GC_fn_type)();
  579.         void * GC_call_with_alloc_lock(GC_fn_type fn, void * client_data);
  580. #   else
  581.         typedef char * (*GC_fn_type)();
  582.         char * GC_call_with_alloc_lock(/* GC_fn_type fn, char * client_data */);
  583. #   endif
  584. # endif
  585.  
  586. /* Check that p and q point to the same object.          */
  587. /* Fail conspicuously if they don't.                */
  588. /* Returns the first argument.                  */
  589. /* Succeeds if neither p nor q points to the heap.        */
  590. /* May succeed if both p and q point to between heap objects.    */
  591. #ifdef __STDC__
  592.   void * GC_same_obj(register void *p, register void *q);
  593. #else
  594.   char * GC_same_obj(/* char * p, char * q */);
  595. #endif
  596.  
  597. /* Check that p is visible                        */
  598. /* to the collector as a possibly pointer containing location.        */
  599. /* If it isn't fail conspicuously.                    */
  600. /* Returns the argument in all cases.  May erroneously succeed        */
  601. /* in hard cases.  (This is intended for debugging use with        */
  602. /* untyped allocations.  The idea is that it should be possible, though    */
  603. /* slow, to add such a call to all indirect pointer stores.)        */
  604. /* Currently useless for multithreaded worlds.                */
  605. #ifdef __STDC__
  606.   void * GC_is_visible(void *p);
  607. #else
  608.   char *GC_is_visible(/* char * p */);
  609. #endif
  610.  
  611. /* Check that if p is a pointer to a heap page, then it points to    */
  612. /* a valid displacement within a heap object.                */
  613. /* Fail conspicuously if this property does not hold.            */
  614. /* Uninteresting with ALL_INTERIOR_POINTERS.                */
  615. /* Always returns its argument.                        */
  616. #ifdef __STDC__
  617.   void * GC_is_valid_displacement(void *p);
  618. #else
  619.   char *GC_is_valid_displacement(/* char * p */);
  620. #endif
  621.  
  622. /* Safer, but slow, pointer addition.  Probably useful mainly with     */
  623. /* a preprocessor.  Useful only for heap pointers.            */
  624. #ifdef GC_DEBUG
  625. #   define GC_PTR_ADD3(x, n, type_of_result) \
  626.     ((type_of_result)GC_same_obj((x)+(n), (x)))
  627. #   ifdef __GNUC__
  628. #       define GC_PTR_ADD(x, n) \
  629.         ((typeof(x))GC_same_obj((x)+(n), (x)))
  630. #   else
  631.     /* We can't do this right without typeof, which ANSI    */
  632.     /* decided was not sufficiently useful.  Repeatedly    */
  633.     /* mentioning the arguments seems too dangerous to be    */
  634.     /* useful.  So does not casting the result.        */
  635. #       define GC_PTR_ADD(x, n) ((x)+(n))
  636. #   endif
  637. #else    /* !GC_DEBUG */
  638. #   define GC_PTR_ADD3(x, n, type_of_result) ((x)+(n))
  639. #   define GC_PTR_ADD(x, n) ((x)+(n))
  640. #endif
  641.  
  642. /* Safer assignment of a pointer to a nonstack location.    */
  643. #ifdef GC_DEBUG
  644. # ifdef __STDC__
  645. #   define GC_PTR_STORE(p, q) \
  646.     (*(void **)GC_is_visible(p) = GC_is_valid_displacement(q))
  647. # else
  648. #   define GC_PTR_STORE(p, q) \
  649.     (*(char **)GC_is_visible(p) = GC_is_valid_displacement(q))
  650. # endif
  651. #else /* !GC_DEBUG */
  652. #   define GC_PTR_STORE(p, q) *((p) = (q))
  653. #endif
  654.  
  655.  
  656. #ifdef SOLARIS_THREADS
  657. /* We need to intercept calls to many of the threads primitives, so     */
  658. /* that we can locate thread stacks and stop the world.            */
  659. /* Note also that the collector cannot see thread specific data.    */
  660. /* Thread specific data should generally consist of pointers to        */
  661. /* uncollectable objects, which are deallocated using the destructor    */
  662. /* facility in thr_keycreate.                        */
  663. # include <thread.h>
  664. # include <signal.h>
  665.   int GC_thr_create(void *stack_base, size_t stack_size,
  666.                     void *(*start_routine)(void *), void *arg, long flags,
  667.                     thread_t *new_thread);
  668.   int GC_thr_join(thread_t wait_for, thread_t *departed, void **status);
  669.   int GC_thr_suspend(thread_t target_thread);
  670.   int GC_thr_continue(thread_t target_thread);
  671.   void * GC_dlopen(const char *path, int mode);
  672.  
  673. # define thr_create GC_thr_create
  674. # define thr_join GC_thr_join
  675. # define thr_suspend GC_thr_suspend
  676. # define thr_continue GC_thr_continue
  677. # define dlopen GC_dlopen
  678.  
  679. /* This returns a list of objects, linked through their first        */
  680. /* word.  Its use can greatly reduce lock contention problems, since    */
  681. /* the allocation lock can be acquired and released many fewer times.    */
  682. void * GC_malloc_many(size_t lb);
  683. #define GC_NEXT(p) (*(void **)(p))     /* Retrieve the next element    */
  684.                     /* in returned list.        */
  685.  
  686. #endif /* SOLARIS_THREADS */
  687.  
  688. /*
  689.  * If you are planning on putting
  690.  * the collector in a SunOS 5 dynamic library, you need to call GC_INIT()
  691.  * from the statically loaded program section.
  692.  * This circumvents a Solaris 2.X (X<=4) linker bug.
  693.  */
  694. #if defined(sparc) || defined(__sparc)
  695. #   define GC_INIT() { extern end, etext; \
  696.                GC_noop(&end, &etext); }
  697. #else
  698. #   define GC_INIT()
  699. #endif
  700.  
  701. #ifdef __cplusplus
  702.     }  /* end of extern "C" */
  703. #endif
  704.  
  705. #endif /* _GC_H */
  706.  
  707.